package com.qf.give_a_like.service.impl;

import com.qf.give_a_like.domain.Crad;
import com.qf.give_a_like.mapper.CradMapper;
import com.qf.give_a_like.service.CradService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class CradServiceImpl implements CradService {

    @Autowired
    CradMapper cradMapper;

    @Override
    @Transactional(rollbackFor = Exception.class,readOnly = true,propagation = Propagation.SUPPORTS)
    public List<Crad> showAll() {
        return cradMapper.showAll();
    }

    @Override
    public int addCrad(Crad card) {
        int i = cradMapper.insertSelective(card);
        return i;
    }

    @Override
    public Crad selectById(Integer id) {
        Crad crad = cradMapper.selectByPrimaryKey(Long.valueOf(id));
        crad.setVisitied(crad.getVisitied() + 1);
        cradMapper.updateByPrimaryKeySelective(crad);
        return crad;
    }

    @Override
    public List<Crad> selectByIds(String likeIds) {
        List<String> strings = Arrays.asList(likeIds.split(","));
        List<Crad> crads = cradMapper.selectByIds(strings);
        return crads;
    }

    @Override
    public Crad selectOneself(Integer id) {
        return cradMapper.selectOneself(id);
    }
}
